<?php

/**
 * Event form.
 *
 * @package    wildlive
 * @subpackage form
 * @author     Your name here
 * @version    SVN: $Id: sfDoctrineFormTemplate.php 23810 2009-11-12 11:07:44Z Kris.Wallsmith $
 */
class EventForm extends BaseEventForm {
	
	protected static $embeddedVenueBeginningAddressFormKey = 'VenueBeginningAddress';
	protected static $embeddedVenueEndAddressFormKey = 'VenueEndAddress';
	
	public function configure() {
		unset ( $this ['participants_list'], $this ['venue_beginning_address_id'], $this ['venue_end_address_id'] );
		$this->__unsetSimpleChangeTrackerFields ();
		
		$this->widgetSchema ['beginning'] = new sfWidgetFormJQueryDate ( array ('config' => '{ }', 'date_widget' => new WidgetFormDateCustomized () ) );
		$this->widgetSchema ['end'] = new sfWidgetFormJQueryDate ( array ('config' => '{}', 'date_widget' => new WidgetFormDateCustomized () ) );
		$this->widgetSchema ['training_id'] = new sfWidgetFormDoctrineChoice ( array ('model' => 'Training', 'add_empty' => true ) );
		$this->widgetSchema ['type'] = new sfWidgetFormChoice ( array ('choices' => array_merge ( array ('' ), Event::getTypes () ) ) );
		
		$this->validatorSchema ['training_id'] = new sfValidatorDoctrineChoice ( array ('required' => false, 'model' => 'Training' ) );
		#$this->validatorSchema ['type'] = new sfValidatorChoice ( array ('choices' => array_merge ( array ('' ), Event::getTypes () ), 'required' => false) );
		
		$firstForm = new ComboAddressForm ($this->getObject()->VenueBeginningAddress);
		$this->embedForm ( self::$embeddedVenueBeginningAddressFormKey, $firstForm );
		$this->embedForm ( self::$embeddedVenueEndAddressFormKey, new ComboAddressForm ($this->getObject()->VenueEndAddress, array('referenceable_form' => $firstForm)) );
	}
	
	public function updateObject($values = null) {
		parent::updateObject ( $values ); // keep this line before the following code block
		
		$forms = $this->getEmbeddedForms ();
		if (is_array ( $forms )) {
			foreach ( $forms as $key => $form ) {
				if ($form instanceof ComboAddressForm) {
					if ($key == self::$embeddedVenueBeginningAddressFormKey) {
						$this->getObject ()->VenueBeginningAddress = $form->getObject ();
					} elseif ($key == self::$embeddedVenueEndAddressFormKey) {
						$this->getObject ()->VenueEndAddress = $form->getObject ();
					}
				}
			}
		}
	}

//	public function save($con = null) {
//		parent::save($con);
//		throw new Exception();
//	}
}
